package com.luocj.looook.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.luocj.looook.R;
import com.luocj.looook.URL;
import com.luocj.looook.adapter.AAdapter;
import com.luocj.looook.base.BaseFragment;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;


public class AFragment extends Fragment {

    private ArrayList<String> strings;
    private String TAG = AFragment.class.getSimpleName();
    private AAdapter aAdapter;
    private View inflate;
    private SmartRefreshLayout smartrefreshlayout;
    private RecyclerView recyclerView;


//    @Override
//    protected int getLayoutId() {
//        return R.layout.fragment_a;
//    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        inflate = inflater.inflate(R.layout.fragment_a, container, false);
        initData();
        initView();
        return inflate;
    }


    protected void initData() {
        strings = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strings.add("itme :" + i);
        }

        OkGo.<String>get(URL.BASE_URL_NBA)
                .tag("tag")
                .params("key", "861a794733dd2b057e415d822bef9586")
                .params("num", 10)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.i(TAG, "onSuccess: " + response.body().toString());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        Log.i(TAG, "onError: " + response.toString());
                    }
                });
    }

    protected void initView() {
        smartrefreshlayout = inflate.findViewById(R.id.smartrefreshlayout);
        recyclerView = inflate.findViewById(R.id.recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(linearLayoutManager);
        aAdapter = new AAdapter(getActivity());
        recyclerView.setAdapter(aAdapter);

        smartrefreshlayout.setEnableAutoLoadmore(true);
        smartrefreshlayout.autoRefresh();

        smartrefreshlayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(final RefreshLayout refreshlayout) {
                Log.i(TAG, "onLoadmore: " + "onloadmore");
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        refreshUI(false, refreshlayout);
                    }
                }, 2000);
            }

            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                refreshlayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshUI(true, refreshlayout);
                    }
                }, 2000);
            }
        });

    }

    private void refreshUI(final boolean b, RefreshLayout refreshlayout) {
        if (b) {
            aAdapter.setNewData(getData());
            smartrefreshlayout.finishRefresh();
            smartrefreshlayout.setLoadmoreFinished(false);
        } else {
            aAdapter.addData(getData());
            smartrefreshlayout.finishLoadmore();
        }
    }

    private ArrayList<String> getData() {
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            strings.add("item :" + i);
        }
        return strings;
    }
}
